package com.ibsol.database
{
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.controls.Alert;
	
	public class SQLExecuteDAO
	{
		//Database connection
		private var _tourixDBConn:SQLConnection;
		
		public function SQLExecuteDAO()
		{
			this._tourixDBConn = DBConnManager.getConnection("tourix");
		}
		
		public function executeSQL(sql:String,resultHandler:Function = null,faultHandler:Function = null):void{
			try{
				var sqlStr:SQLStatement = new SQLStatement();
				sqlStr.sqlConnection = _tourixDBConn;
				sqlStr.text = sql;
				sqlStr.addEventListener(SQLEvent.RESULT,
					function (e:SQLEvent):void{
						if(resultHandler != null){
							resultHandler.call(this);
						}
					});
				sqlStr.addEventListener(SQLErrorEvent.ERROR,faultHandler == null ? sqlErrorHandler : faultHandler);
				sqlStr.execute();
			}catch(e:SQLError){
				//trace("Errors:"+e.message+e.getStackTrace());
				throw e;
			}
		}
		
		private function sqlErrorHandler(e:SQLErrorEvent):void{
			trace("error:"+e.text);
			mx.controls.Alert.show(e.text, "Error");
		}

	}
}